package com.wlit.ui;

import com.wlit.WlitBrowserApp;
import com.wlit.print.LabelPrinter;
import com.wlit.tianping.TianpingService;
import org.cef.CefApp;
import org.cef.browser.CefBrowser;
import org.cef.browser.CefFrame;
import org.cef.callback.CefContextMenuParams;
import org.cef.callback.CefMenuModel;
import org.cef.handler.CefContextMenuHandler;

import javax.swing.*;

public class MenuHandler implements CefContextMenuHandler {

    private static final int ID_BACK = CefMenuModel.MenuId.MENU_ID_USER_LAST + 1;
    private static final int ID_FORWARD = CefMenuModel.MenuId.MENU_ID_USER_LAST + 2;
    private static final int ID_RELOAD = CefMenuModel.MenuId.MENU_ID_USER_LAST + 3;
    private static final int ID_TIANPING = CefMenuModel.MenuId.MENU_ID_USER_LAST + 4;
    private static final int ID_EXIT = CefMenuModel.MenuId.MENU_ID_USER_LAST + 5;
    private static final int ID_DEVTOOLS = CefMenuModel.MenuId.MENU_ID_USER_LAST + 6;
    private static final int ID_ABOUT = CefMenuModel.MenuId.MENU_ID_USER_LAST + 7;
    private static final int ID_PRINT_CONFIG = CefMenuModel.MenuId.MENU_ID_USER_LAST + 8;

    private CefBrowser browser;

    public MenuHandler(CefBrowser browser) {
        this.browser = browser;
    }

    @Override
    public void onBeforeContextMenu(CefBrowser cefBrowser, CefFrame cefFrame, CefContextMenuParams cefContextMenuParams, CefMenuModel cefMenuModel) {
        cefMenuModel.clear();
        cefMenuModel.addItem(ID_BACK, "后退");
        cefMenuModel.addItem(ID_FORWARD, "前进");
        cefMenuModel.addItem(ID_RELOAD, "刷新");
        cefMenuModel.addSeparator();
        cefMenuModel.addItem(ID_TIANPING, "天平配置");
        cefMenuModel.addSeparator();
        cefMenuModel.addItem(ID_PRINT_CONFIG, "打印配置");
        cefMenuModel.addSeparator();
        cefMenuModel.addItem(ID_DEVTOOLS, "开发者工具");
        cefMenuModel.addItem(ID_ABOUT, "关于");
        cefMenuModel.addSeparator();
        cefMenuModel.addItem(ID_EXIT, "退出系统");

    }

    @Override
    public boolean onContextMenuCommand(CefBrowser cefBrowser, CefFrame cefFrame, CefContextMenuParams cefContextMenuParams, int i, int i1) {
        switch (i) {
            case ID_BACK:
                browser.goBack();
                return true;


            case ID_FORWARD:
                browser.goForward();
                return true;

            case ID_RELOAD:
                browser.reload();
                return true;

            case ID_TIANPING:
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        TianpingDialog dialog = new TianpingDialog(WlitBrowserApp.mainFrame);
                        WlitBrowserApp.dataListeners.add(dialog);
                        dialog.setVisible(true);
                    }
                });

                return true;

            case ID_EXIT:
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        int result = JOptionPane.showConfirmDialog(WlitBrowserApp.mainFrame, "确定要退出吗？", "提示", JOptionPane.YES_NO_OPTION);
                        if (result == JOptionPane.NO_OPTION) {
                            return;
                        }
                        if (result == JOptionPane.YES_OPTION) {
                            if (TianpingService.getInstance().isOpen()) {
                                TianpingService.getInstance().closeComm();
                            }
                            LabelPrinter.getInstance().close();
                            CefApp.getInstance().dispose();
                            WlitBrowserApp.mainFrame.dispose();
                        }
                    }
                });
                return true;


            case ID_DEVTOOLS:
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                         DevToolsDialog dialog = new DevToolsDialog(WlitBrowserApp.mainFrame, "开发者工具", browser);
                         dialog.setVisible(true);
                    }
                });
                return true;

            case ID_ABOUT:
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        AboutDialog dialog = new AboutDialog(WlitBrowserApp.mainFrame);
                        dialog.setVisible(true);
                    }
                });
                return true;

            case ID_PRINT_CONFIG:
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                       PrintDialog dialog = new PrintDialog(WlitBrowserApp.mainFrame);
                       dialog.setVisible(true);
                    }
                });
                return true;
        }
        return false;
    }

    @Override
    public void onContextMenuDismissed(CefBrowser cefBrowser, CefFrame cefFrame) {

    }
}
